package cn.dida.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import cn.dida.model.Brand;
import cn.dida.model.Pageable;

/**
 * 品牌管理 数据交互类
 * @author weiwei
 */
@Repository
public class BrandDao extends DaoBase {

	/**
	 * 品牌列表
	 * @param companyId 公司id
	 * @param keyword 查询条件
	 * @param state 状态（1：正常、2：已停用）
	 * @param page 分页游标
	 * @return Pageable<Brand> 品牌分页列表
	 */
    public Pageable<Brand> query(Integer companyId, String keyword, Integer state, int page) {
        Object[] args = {};
        StringBuffer sqlQueryResultCount = new StringBuffer("select count(1) from brand brd join staff stf on brd.create_staff_id = stf.id ");
        StringBuffer sqlQueryResult = new StringBuffer("select brd.*,stf.id 'createStaffId',stf.account 'createStaffAccount' from brand brd join staff stf on brd.create_staff_id = stf.id");

        StringBuffer sqlWhere = new StringBuffer(" where brd.company_id = ?");
        args = ArrayUtils.add(args, companyId);
        if (state != null) {
            sqlWhere.append(" and brd.state = ?");
            args = ArrayUtils.add(args, state);
        }
        if (keyword != null) {
            sqlWhere.append(" and brd.name like ?");
            args = ArrayUtils.add(args, "%" + keyword + "%");
        }

        // 查询记录总数
        Integer resultTotal = jdbcTemplate.queryForObject(sqlQueryResultCount.append(sqlWhere).toString(), Integer.class, args);
        // 查询记录
        sqlQueryResult.append(sqlWhere).append("  order by create_time desc limit ?,?");
        args = ArrayUtils.add(args, (page - 1) * getPageSize());
        args = ArrayUtils.add(args, getPageSize());
        List<Brand> result = jdbcTemplate.query(sqlQueryResult.toString(), args, new BeanPropertyRowMapper<>(Brand.class));
        return new Pageable<>(page, getPageSize(), resultTotal, result);
    }

    /**
     * 品牌列表
     * @param companyId 公司id
     * @param state 状态（1：正常、2：已停用）
     * @return List<Brand> 品牌列表
     */
    public List<Brand> query(Integer companyId, Integer state) {
        String sql = "select brd.*,stf.id 'createStaffId',stf.account 'createStaffAccount' from brand brd join staff stf on brd.create_staff_id = stf.id where brd.company_id = ? and brd.state = ?";
        List<Brand> result = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(Brand.class), companyId, state);
        return result;
    }

    /**
     * 新增品牌
     * @param brand 品牌实体信息
     * @return int 受影响的记录行数
     */
    public int insert(Brand brand) {
        String sql = "insert into brand(name, company_id, state, create_time, create_staff_id) values(:name, :company_id, :state, :create_time, :create_staff_id)";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("company_id", brand.getCompanyId());
        paramMap.put("name", brand.getName());
        paramMap.put("state", brand.getState());
        paramMap.put("create_time", new Date());
        paramMap.put("create_staff_id", brand.getCreateStaffId());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }
    /**
     * 更新品牌
     * @param brand 品牌实体信息
     * @return int 受影响的记录行数
     */
    public int update(Brand brand) {
        String sql = "update brand set name=:name , state=:state where company_id = :company_id and id = :id";
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", brand.getId());
        paramMap.put("company_id", brand.getCompanyId());
        paramMap.put("name", brand.getName());
        paramMap.put("state", brand.getState());
        return namedParameterJdbcTemplate.update(sql, paramMap);
    }
    /**
     * 根据公司id和品牌id获取品牌信息
     * @param companyId 公司id
     * @param id 品牌id
     * @return Brand 品牌信息
     */
    public Brand get(Integer companyId, Integer id) {
        String sql = "select * from brand where company_id = ? and id = ?";
        try {
            Brand brand = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Brand.class), companyId, id);
            return brand;
        } catch (DataAccessException e) {
            return null;
        }
    }

    /**
     * 根据公司id和品牌名称获取品牌信息
     * @param companyId 公司id
     * @param name 品牌名称
     * @return Brand 品牌信息
     */
    public Brand getByName(Integer companyId, String name) {
        String sql = "select * from brand where company_id = ? and name = ?";
        try {
            Brand brand = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(Brand.class), companyId, name);
            return brand;
        } catch (DataAccessException e) {
            return null;
        }
    }
    /**
     * 删除品牌
     * @param companyId 公司id
     * @param id 品牌id
     * @return int 受影响的记录行数
     */
    public int delete(Integer companyId, Integer id) {
        String sql = "delete from brand where company_id = ? and id = ?";
        return jdbcTemplate.update(sql, companyId, id);
    }
}
